# demo05_generator.py
# 创建一个生成器对象
"""
# 1.通过类似于列表推导—— 生成器表达式
gen = (i for i in range(1, 6))
# <generator object <genexpr> at 0x7f8b67ffec80>
# print(gen)
# 从生成器对象中获取一个值

print('数据1：', gen.__next__())
print('数据2：', gen.__next__())
# 通过for循环
for item in gen:
    print('for:', item)
# 只能获取一遍
# print(gen.__next__())
"""
# 2.生成器函数
# 调用函数，每次得到的结果不一样
def gen02():
    yield 2
    yield 'hello'
    yield True
    yield [1, 2, 3, 4]
    yield {'a': 5, 'b': 6}
# print(gen02())
# print(gen02())
# <generator object gen02 at 0x7f45eb221c80>
g2 = gen02()
# print(g2)
print(g2.__next__())
print(g2.__next__())
for item in g2:
    print('for:', item)

